package com.itwjr.jdbc2.Test1;

import java.sql.*;

public class Demo3 {
    public static void main(String[] args) throws Exception {
        //查询


        //注册驱动
        Class.forName("com.mysql.jdbc.Driver") ;

        //获取连接
        String url = "jdbc:mysql://127.0.0.1:3306/db1?useSSL=false";

        String user = "root";
        String psw = "1234";
        Connection connection = DriverManager.getConnection(url, user, psw);

        String userName = "张三";
        //定义sql语句
        String sql = "select * from user where name=?";
        //获取statement对象,包装sql语句
        PreparedStatement preparedStatement = connection.prepareStatement(sql);


        //告诉preparedStatement那个位置的参数是
        preparedStatement.setString(1,userName);

        //执行select语句  返回结果,返回一个结果集
        ResultSet resultSet = preparedStatement.executeQuery();



        //判断所查询的结果是否存在
        while (resultSet.next()){
            int id = resultSet.getInt(1);
            System.out.println(id);
            String name = resultSet.getString(2);
            System.out.println(name);
            int age = resultSet.getInt(3);
            System.out.println(age);


        }

        //关闭资源
        resultSet.close();
        preparedStatement.close();
        connection.close();


    }
}
